#!/usr/bin/env python
#+---------------------------------------------+
#| ProxyBot 0.7
#+---------------------------------------------+
#| Written by Alec Hussey
#| Copyright (C) 2008 MadSoft
#| Website: www.madsoft.org
#| License: GNU General Public License v3
#+---------------------------------------------+

from proxybot import *
import sys, random

help_message = """Usage: %s [OPTIONS] <server> <channel>
Options:
\t-n,--nick <nick>          Define a specific nick
\t-c,--config <path>        Path to custom proxybot.conf
\t-s,--nickserv <password>  Identify with nickserv password
\t-h,--help                 Display this help information"""

if __name__ == "__main__":
	# Check to make sure all arguments were passed
	if len(sys.argv) >= 3:
		# Initialize base variables
		server = None
		channel = None
		nick = "ProxyBot%d" % random.randint(1000, 9999)
		nickserv = None
		config = "proxybot.conf"
		
		"""
		Parse command line arguments by looping through
		from the second element to the end of the list.
		Skip two elements to account for the server and
		channel arguments, we only want to parse the options
		"""
		num = 0
		while num < len(sys.argv):
			# Interpret command line options
			if sys.argv[num] == "-n" or sys.argv[num] == "--nick":
				# Check for nick argument
				if len(sys.argv[num+1]) > 0:
					nick = sys.argv[num+1]
				else:
					print "Error: Invalid argument syntax for %s" % sys.argv[num]
			elif sys.argv[num] == "-c" or sys.argv[num] == "--config":
				# Check for config argument
				if len(sys.argv[num+1]) > 0:
					config = sys.argv[num+1]
				else:
					print "Error: Invalid argument syntax for %s" % sys.argv[num]
			elif sys.argv[num] == "-s" or sys.argv[num] == "--nickserv":
				# Check for password argument
				if len(sys.argv[num+1]) > 0:
					nickserv = sys.argv[num+1]
				else:
					print "Error: Invalid argument syntax for %s" % sys.argv[num]
			elif sys.argv[num] == "-h" or sys.argv[num] == "--help":
				print help_message % sys.argv[0]
				sys.exit(1)
			num += 1
		
		"""
		Extract server and channel names simply by
		taking the values of the last two elements
		of the list respectively
		"""
		server = sys.argv[-2]
		channel = sys.argv[-1]
		
		# Create new ProxyBot object and start it up
		print "Starting bot... ",
		ProxyBot(server, channel, nick, nickserv, config).start()
		print "Done"
	else:
		print help_message % sys.argv[0]
		sys.exit(1)
